//
//  UIViewController+PrepareEx.swift
//  APP
//
//  Created by Rdxer on 2017/12/9.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import FastComponent


extension UIViewController:XXEmptyDataSet{
    
    public func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
//        return #imageLiteral(resourceName: "data_developing_image")
        return  xx.emptyDataSetStatu?.image
    }
    
    public func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        return xx.emptyDataSetStatu?.title
    }
    
    public func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
        return xx.emptyDataSetStatu?.backgroundColor
    }
    
    public func emptyDataSet(_ scrollView: UIScrollView, didTap button: UIButton) {
        
    }
    
    public func emptyDataSet(_ scrollView: UIScrollView, didTap view: UIView) {
        
    }
    
    public func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
}



public extension UIViewController{
    public func prepareEmptyDataSetIfNeed(scrollView:UIScrollView){
        if !(self.xx.notNeedPrepareEmptyDataSet ?? false){
            prepareEmptyDataSet(scrollView: scrollView)
            self.xx.notNeedPrepareEmptyDataSet = true
        }
    }
}

// MARK: - 添加属性
// key ----
private var XXUIViewController_Prepare_XXEmptyDataSet_ifNeed_Key: Void?
private var XXUIViewController_Prepare_XXEmptyDataSet_statu: Void?

public extension XXExtension where Base:UIViewController{
    fileprivate var notNeedPrepareEmptyDataSet:Bool? {
        get {
            return objc_getAssociatedObject(base, &XXUIViewController_Prepare_XXEmptyDataSet_ifNeed_Key) as? Bool
        }
        set {
            objc_setAssociatedObject(base, &XXUIViewController_Prepare_XXEmptyDataSet_ifNeed_Key, newValue, .OBJC_ASSOCIATION_ASSIGN)
        }
    }
    
    public var emptyDataSetStatu:XXEmptyDataSetStatu? {
        get {
            if let s = objc_getAssociatedObject(base, &XXUIViewController_Prepare_XXEmptyDataSet_statu) as? XXEmptyDataSetStatu{
                return s
            }else{
                self.emptyDataSetStatu = XXEmptyDataSetStatu.developing
                return XXEmptyDataSetStatu.developing
            }
        }
        set {
            objc_setAssociatedObject(base, &XXUIViewController_Prepare_XXEmptyDataSet_statu, newValue, .OBJC_ASSOCIATION_RETAIN)
        }
    }
}

public enum XXEmptyDataSetStatu {
    /// 加载中
    case loading
    /// 加载失败
    case loadingError
    /// 加载成功 - 空数据
    case emptyData
    /// 网络错误
    case networkError
    /// 开发中
    case developing // default
}


extension XXEmptyDataSetStatu{
    var image:UIImage? {
        switch self {
        case .loading:return nil
        case .loadingError:return nil
        case .emptyData:return #imageLiteral(resourceName: "data_empty_image")
        case .networkError:return nil
        case .developing:return #imageLiteral(resourceName: "data_developing_image")
        }
    }
    
    var title:NSAttributedString? {
        
        let makeAttStrClosure = { (text:String) -> NSAttributedString? in
            let font = UIFont.systemFont(ofSize: 16)
            let textColor = AppColor.lightTextColor
            return NSAttributedString.init(string: text, attributes: [
                NSAttributedStringKey.font:font,
                NSAttributedStringKey.foregroundColor:textColor
                ])
        }
        
        switch self {
        case .loading:return makeAttStrClosure("加载中...")
        case .loadingError:return makeAttStrClosure("加载失败")
        case .emptyData:return makeAttStrClosure("当前无数据")
        case .networkError:return makeAttStrClosure("网络错误")
        case .developing:return makeAttStrClosure("   \n    ")
        }
    }
    var backgroundColor:UIColor? {
        switch self {
//        case .loading:return nil
//        case .loadingError:return nil
//        case .emptyData:return nil
//        case .networkError:return nil
//        case .developing:return nil
        default: return AppColor.backgroundColor
        }
    }
    
    
}



